﻿using sMES_Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using sMES_Base.service;
using System.Data;
using CUS_Product_QCList_Get_Process.Modules;
using System.Xml;
using CUS_Product_QCList_Get_Process.ORM;

namespace CUS_Product_QCList_Get_Process
{
    /// <summary>
	/// 元件[CUS_Product_QCList_Get_Process]
    /// <auth>gracecai</auth><time>2018/12/4 下午 03:40:57</time>
    /// <description></description>
    /// <list>
    ///     <modify>
    ///         <auth></auth><mantis></mantis><time></time>
    ///         <description></description>
    ///     </modify>
    /// </list>
    /// </summary>
    public class CUS_Product_QCList_Get_Process_utility : BaseUtility
    {
        public CUS_Product_QCList_Get_Process_utility(ref BaseService CurrentService) : base(ref CurrentService)
        {

        }

        public ResponseContent initProductQCList(RequestContent content)
        {
            ResponseContent result = new ResponseContent();

            StringBuilder _sql = new StringBuilder();

            // 1.查询产品编号
            _sql.Clear();
            _sql.Append($" select productno from TBLWIPLOTBASIS WHERE baselotno = '{content.plotNo}' ");
            List<string> strPrd = CurrentService.GetDao().Query<string>(_sql.ToString());
            if (strPrd.Count == 0)
                throw new ServiceException(ServiceCode.ParamsValidateException, $"生产批号{content.plotNo}不存在基本资料");
            result.productno = strPrd[0];

            // 2.查询产品对应的点检表信息
            _sql.Clear();
            _sql.Append($@" select a.opno,b.qclistno,b.qclistname,b.qctype
                            from TBLCUSProductQCListLink a
                              join TBLEQPQCLISTBASIS b on a.qclistno = b.qclistno
                              join TBLWIPLOTBASIS c on a.productno = c.productno and a.productversion = c.productversion
                            where a.linkstate > 0 and c.baselotno = '{content.plotNo}'
                             and (opno = '' or opno is null or opno = '{content.opNo}') ");
            result.qcLists = CurrentService.GetDao().Query<ORM_QCList>(_sql.ToString());

            if (result.qcLists.Count() == 0)
                throw new ServiceException(ServiceCode.ParamsValidateException, $"产品编号{result.productno} 暂未设定工艺点检信息");
            
            result.qcDetails = GetQCDetails(result.productno, result.qcLists.First().qclistno);
            return result;
        }

        public ResponseContent initProductQCDetail(RequestContent content)
        {
            ResponseContent result = new ResponseContent();
            result.qcDetails = GetQCDetails(content.productno, content.qclistno);
            if (result.qcDetails.Count == 0)
                throw new ServiceException(ServiceCode.ParamsValidateException, $"产品编号：{content.productno}，点检表：{content.qclistno}，未设定点检明细！");
            return result;
        }

        public List<ORM_QCDetail> GetQCDetails(string productno, string qclistno)
        {
            StringBuilder _sql = new StringBuilder();
            _sql.AppendLine($@" Select det.QcOrder AS item_no,det.QcOrder AS seq,det.QcItem AS check_item,det.QcType+1 AS check_type,det.StdValue AS std_value,
                                       det.MinIvalue AS range_min,det.MaxIvalue AS range_max,det.InspectionMethod AS check_method, 
                                       det.InspectionStandards AS check_standard,det.AutoDataQuery as auto_data_query,
                                       '' AS param_no,'' AS data_item_no,'N' AS is_param,InputDataCount
                                From TBLCUSProductQCListLink cf
                                   Join tblEQPQCLISTBASIS qcl On cf.QcListNo = qcl.QCListNo
                                   Join tblEQPQCLISTDETAIL det On qcl.QCLISTNO = det.QCLISTNO 
                                Where cf.productno = '{productno}' and cf.qclistno = '{qclistno}'");
            return CurrentService.GetDao().Query<ORM_QCDetail>(_sql.ToString());
        }
    }
}
